/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.component.analyzer.rest.gold;

import com.example.common.api.base.response.ResponseJson;
import com.example.common.api.controlller.BaseApiController;
import com.example.common.api.exception.AbstractException;
import com.example.common.spring.util.RestClient;
import com.example.component.analyzer.common.constant.RefRestApiConstant;
import com.example.component.analyzer.common.dto.GoldChartDTO;
import com.example.component.analyzer.common.dto.GoldResponseDTO;
import com.example.component.analyzer.common.enums.GoldChartDataTypeEnum;
import com.example.component.analyzer.core.model.Gold;
import com.example.component.analyzer.service.gold.GoldDataAnalysisService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-07-02 21:15
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-07-02
 * @modify reason: {方法名}:{原因}
 * ...
 */
@RestController
@RequestMapping(value = "/api/analyzer/gold")
public class GoldRestController extends BaseApiController {

    /**
     * 黄金数据分析服务
     */
    @Resource
    private GoldDataAnalysisService goldDataAnalysisService;

    /**
     * 查询当前数据
     * @return
     */
    @GetMapping("/current")
    public ResponseJson current() {
        return ResponseJson.newResponse().ok(RestClient.getInstance().exchange(RefRestApiConstant.REST_GOLD_OPEN_API, HttpMethod.GET, GoldResponseDTO.class));
    }

    /**
     * 保存数据
     * @return
     */
    @GetMapping("/save")
    public ResponseJson<Gold> save() {
        GoldResponseDTO result = RestClient.getInstance().exchange(RefRestApiConstant.REST_GOLD_OPEN_API, HttpMethod.GET, GoldResponseDTO.class);
        Gold gold = new Gold();
        BeanUtils.copyProperties(result.getBody(), gold);
        goldDataAnalysisService.save(gold);
        return ResponseJson.<Gold>newResponse().ok(gold);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/find")
    public ResponseJson<List<Gold>> find() {
        return ResponseJson.<List<Gold>>newResponse().ok(goldDataAnalysisService.findAll());
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseJson delete(Long id) {
        goldDataAnalysisService.deleteById(id);
        return ResponseJson.newResponse().ok();
    }

    /**
     * 根据表格类型加载数据
     * @param type
     * @return
     */
    @GetMapping("/load/{type}")
    public ResponseJson<GoldChartDTO> load(@PathVariable Integer type) {
        ResponseJson<GoldChartDTO> result = ResponseJson.<GoldChartDTO>newResponse().ok();
        try{
            super.assertNotNull(type,"type");
            super.assertNotIllegalRange(type,
                    GoldChartDataTypeEnum.codes()
                    ,"type");
            result.ok(goldDataAnalysisService.loadChartDataWithType(type));
        }catch (AbstractException e){
            super.handleException(result,e);
        }catch (Throwable e){
            super.handleException(result,e);
        }
        return result;
    }
}
